package thread.ABC;

public class PrintABC {

    private static int count = 0;

    public static void main(String[] args) {
        String str = "ABC";
        PrintABC printABC = new PrintABC();
        ThreadA threadA = printABC.new ThreadA(str);
        ThreadB threadB = printABC.new ThreadB(str);
        ThreadC threadC = printABC.new ThreadC(str);

        threadC.start();
        threadB.start();
        threadA.start();

    }

    /**
     * 打印A
     * @author LKB
     *
     */
    class ThreadA extends Thread{

        private String str;

        public ThreadA(String str) {
            // TODO Auto-generated constructor stub
            this.str = str;
        }

        public void run(){
            while(true){
                synchronized (str) {
                    while(count%3 == 0){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+"----A");
                        count++;
                    }
                }
            }
        }
    }

    /**
     * 打印B
     * @author LKB
     *
     */
    class ThreadB extends Thread{

        private String str;

        public ThreadB(String str) {
            // TODO Auto-generated constructor stub
            this.str = str;
        }

        public void run(){
            while(true){
                synchronized (str) {
                    while(count%3 == 1){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+"----B");
                        count++;
                    }
                }
            }
        }
    }

    /**
     * 打印C
     * @author LKB
     *
     */
    class ThreadC extends Thread{

        private String str;

        public ThreadC(String str) {
            // TODO Auto-generated constructor stub
            this.str = str;
        }

        public void run(){
            while(true){
                synchronized (str) {
                    while(count%3 == 2){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName()+"----C");

                        System.out.println("++++++");

                        count++;
                    }
                }
            }
        }
    }


}
